



#settings
tfinal=240               #in Hours
timedecimalplaces=2



#variables

#x =>Control/natural N
#x1->N
#x2->E
#x3->A
#x4->P
x1init=1.25*10**3           #cfu/ml
x2init=(0 )*10**-9             #Molar
x3init=(0 )*10**-9            #Molar
#Reporter
x4init=(0)                      #Molar



xinit=x1init

#constants
d=4.0/1000       #1/nM.h
de=2.0           #1/h
va=4.8*10**-7    #nM*ml/h
ke=5.0           #1/h

pigScale=1
pigThresh= (100.0/(536.873*10**3) ) /10    #Molar (conc of lycopene in tomato)/10
E_leak=0.01                               #ratio of leak expression to normal
A_leak=0.01

valuesatpH= {6.2:(0.885,1.25,0.274), 6.6:(.928,1.17,.304),7.0:(.970,1.24,.639),7.4:(.897,1.16,0.791),7.8:(.936,1.20,1.19)}     #from paper



#parameters
pH=6.6





import numpy as np
import scipy.integrate
import pickle
import math

#Setting values
k,x1max,da=valuesatpH[pH]
x1max*=10**9        #cfu/ml



#Definitions
def dxdt(x,t=0):
    """
    Derivative of x 
    """
    return k*x*(1-1.0*x/x1max)



#These assume that:
#1 The terminator gene production is inactive till pigment reaches a certain conc(pigThresh)
#2 The the pigment production is proportional to the AHL (A/x3) with proportionality constant (pigScale).
#3 No degradation of pigment.
def dx1dt(x1,x2,x3,x4):
    """
    Derivative of x1
    """
    return k*x1*(1-1.0*x1/x1max)-d*x2*x1

def dx2dt(x1,x2,x3,x4):
    """
    Derivative of x2
    """
    
    

    return ke*x3-de*x2
    
def dx3dt(x1,x2,x3,x4):
    """
    Derivative of x3
    """
    if (x4<=pigThresh*10**9):     #10**-9 is to convert to nM
        return  A_leak*va*x1-da*x3
    else:
        return va*x1-da*x3
def dx4dt(x1,x2,x3,x4):
    """
    Derivative of x4
    """
    return pigScale*(va*x1)

def drdt(r,t=0):
    """
    Combined dx1dt,dx2dt,dx3dt for odeint
    **Make changes to derivatives in their respective functions only**
    """
    return np.array([dx1dt(r[0],r[1],r[2],r[3]),dx2dt(r[0],r[1],r[2],r[3]),dx3dt(r[0],r[1],r[2],r[3]),dx4dt(r[0],r[1],r[2],r[3])])








#setting up
r0=np.array([x1init,x2init,x3init,x4init])    
t = np.linspace(0,tfinal ,1+tfinal*10**timedecimalplaces )# time



#run integral
R= scipy.integrate.odeint(drdt, r0, t)
Rc=scipy.integrate.odeint(dxdt, xinit, t)

#sort data
print R
N,E,A,P=R.T  #r.transpose
Nc,=Rc.T


#Saving data
pickle.dump((t,R),open( "Result QsPig init"+str(r0)+"pH"+str(pH)+".p", "w" ))
pickle.dump((t,Rc),open( "Control init"+str(xinit)+"pH"+str(pH)+".p", "w" ))

#plot
import pylab
sav=pylab.figure(1)
pylab.plot(t,N)
pylab.title("N vs t")
#pylab.legend(('x'))
pylab.xlabel("t in hours")
pylab.ylabel('N cfu/ml')
sav.savefig('N_data'+str(r0)+"pH"+str(pH)+".png")
pylab.show()

sav=pylab.figure(2)
pylab.plot(t,E, 'r-', label='E')
pylab.title("E vs t")
pylab.legend(('E'))
pylab.xlabel("t in hours")
pylab.ylabel('E')
sav.savefig('E_data'+str(r0)+"pH"+str(pH)+".png")
pylab.show()


sav=pylab.figure(3)
pylab.plot(t,A)
pylab.title("A vs t")
pylab.xlabel("t in hours")
pylab.ylabel('A')
sav.savefig('A_data'+str(r0)+"pH"+str(pH)+".png")
pylab.show()

sav=pylab.figure(4)
pylab.plot(t,P)
pylab.title("Pigment vs t")
pylab.xlabel("t in hours")
pylab.ylabel('Pigment nM')
sav.savefig('Pig_data'+str(r0)+"pH"+str(pH)+".png")
pylab.show()

sav=pylab.figure(5)
pylab.title("N vs Nc"+"\ninitial pos:"+str(r0)+'\n')
pylab.plot(t, N, 'r-', label='N')
pylab.plot(t, Nc, 'y-', label='Nc')
pylab.grid()
pylab.legend(loc='best')
pylab.xlabel('time (h)')
pylab.ylabel('cfu/ml')
sav.savefig('N vs Nc'+str(r0)+"pH"+str(pH)+".png")
pylab.show()

sav2=pylab.figure(6)
pylab.title("N vs P initial pos:"+str(r0))
pylab.ylabel('Viable Cell Density (cfu/ml)')
pylab.plot(t, N, 'r-', label='N (cfu/ml)')
pylab.legend(loc='best')
pylab.twinx()
pylab.ylabel('nM')
pylab.plot(t,P, 'g-', label='P (nM)')
#pylab.plot(t,E, 'g-', label='E  (nM)')
pylab.grid()
#pylab.legend(loc='best')
pylab.xlabel('time (h)')

"""sav2.savefig('combined_dataNall'+str(r0)+"pH"+str(pH)+'.png')
pylab.show()

pylab.figure(3)
pylab.plot(tdelayed,Ad,label='delayed')
pylab.legend()
pylab.figure(2)
pylab.plot(tdelayed,Ed,label='delayed')
pylab.legend()
#pylab.figure(1)
#pylab.plot(t,Nd,label='delayed')"""